package com.cskaoyan.service.wx;

import com.cskaoyan.bean.bo.wjjBO.WXUserIndexBO;
import com.cskaoyan.bean.common.Constant;
import com.cskaoyan.bean.marketBean.*;
import com.cskaoyan.bean.po.GoodsCountGroupByBrandId;
import com.cskaoyan.bean.vo.wjjVO.WXHomeIndexFloorGoodsListVO;
import com.cskaoyan.bean.vo.wjjVO.WXHomeIndexVO;
import com.cskaoyan.bean.vo.wjjVO.WXUserIndexVO;
import com.cskaoyan.bean.vo.wjjVO.WXUserPersonalVO;
import com.cskaoyan.mapper.marketMapper.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wjj
 * @date 2022-12-31 11:06
 * @describe
 */
@Service
@Transactional
public class WXUserServiceImpl implements WXUserService {

    @Autowired
    MarketAdMapper adMapper;

    @Autowired
    MarketCategoryMapper categoryMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    @Autowired
    MarketCommentMapper commentMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketBrandMapper brandMapper;

    @Autowired
    MarketTopicMapper topicMapper;

    @Override
    public WXHomeIndexVO displayHomePage() {

        Integer limit = Constant.WX_GOODS_LIMIT_CODE;
        List<MarketAd> banner = adMapper.selectMarketAd();
        MarketCategory marketCategory = new MarketCategory();
        Integer fatherCategoryCid = Constant.WX_GOODS_FATHERCATEGORY_CID_CODE;
        marketCategory.setPid(fatherCategoryCid);
        List<MarketCategory> channel = categoryMapper.selectMarketMainCategory(marketCategory);

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("mainCategoryList", channel);

        List<MarketCoupon> couponList = couponMapper.selectMarketCoupon();

        List<GoodsCountGroupByBrandId> goodsCountList = goodsMapper.selectGoodsCountBrandOrderByCount();

        List<MarketBrand> brandList = new ArrayList<>();

        for (GoodsCountGroupByBrandId goodsCountGroupByBrandId : goodsCountList) {
            MarketBrand marketBrand = brandMapper.selectMarketBrandByIdLimit(goodsCountGroupByBrandId.getBrand_id(),limit);
            brandList.add(marketBrand);
        }

        List<WXHomeIndexFloorGoodsListVO> floorGoodsList = new ArrayList<>();

        for (MarketCategory category : channel) {
            WXHomeIndexFloorGoodsListVO floorGoodsListVO = new WXHomeIndexFloorGoodsListVO();
            List<MarketGoods> goodsList = goodsMapper.selectMarketGoodsByCategoryIdLimit(category.getId(),limit);
            floorGoodsListVO.setGoodsList(goodsList);
            floorGoodsListVO.setId(category.getId());
            floorGoodsListVO.setName(category.getName());
            floorGoodsList.add(floorGoodsListVO);
        }

        List<MarketGoods> hotMarketGoodsList = goodsMapper.selectHotMarketGoodsLimit(limit);
        List<MarketGoods> newMarketGoodsList = goodsMapper.selectNewMarketGoodsLimit(limit);

        List<MarketTopic> topicList = topicMapper.selectMarketTopic(limit);

        WXHomeIndexVO homeIndexVO = new WXHomeIndexVO();
        homeIndexVO.setBanner(banner);
        homeIndexVO.setChannel(channel);
        homeIndexVO.setCouponList(couponList);
        homeIndexVO.setBrandList(brandList);
        homeIndexVO.setFloorGoodsList(floorGoodsList);
        homeIndexVO.setHotGoodsList(hotMarketGoodsList);
        homeIndexVO.setNewGoodsList(newMarketGoodsList);
        homeIndexVO.setTopicList(topicList);

        return homeIndexVO;
    }

    @Override
    public WXUserIndexVO selectPersonal(WXUserIndexBO wxUserIndexBO) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setUserId(wxUserIndexBO.getId());
        List<MarketOrder> orderList = orderMapper.selectOrderByUserId(marketOrder.getUserId());

        int uncomment = 0;
        int unpaid = 0;
        int unrecv = 0;
        int unship = 0;

        for (MarketOrder order : orderList) {
            Integer orderId = order.getId();
            MarketOrderGoods orderGoods = new MarketOrderGoods();
            orderGoods.setOrderId(orderId);
            orderGoods.setComment(0);
            List<MarketOrderGoods> oGoods = orderGoodsMapper.selectMarketOrderGoods(orderGoods);

            int orderStatus = order.getOrderStatus();
            if (orderStatus == Constant.UNPAID_CODE) {
                unpaid++;
            } else if (orderStatus == Constant.PAID_CODE) {
                unship++;
            } else if (orderStatus == Constant.DELIVER_CODE) {
                unrecv++;
            } else if (orderStatus == Constant.USER_GET_PACKAGE_CODE || orderStatus == Constant.SYSTEM_GET_PACKAGE_CODE) {
                uncomment += oGoods.size();
            }
        }
        WXUserPersonalVO wxUserPersonalVO = new WXUserPersonalVO();
        wxUserPersonalVO.setUncomment(uncomment);
        wxUserPersonalVO.setUnpaid(unpaid);
        wxUserPersonalVO.setUnrecv(unrecv);
        wxUserPersonalVO.setUnship(unship);
        WXUserIndexVO userIndexVO = new WXUserIndexVO();
        userIndexVO.setOrder(wxUserPersonalVO);
        return userIndexVO;
    }
}
